﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Entity;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.Scenes.Basic;
using Spinning.Spinning.Scenes.ProcessScene;
using Microsoft.Xna.Framework.Input.Touch;
using Spinning.Spinning.Entity.Scenes;
using Spinning.Spinning.Entity.Scenes.SinglePlayer;

namespace Spinning.Spinning.UI
{
    class StageItemListBar : BaseScene
    {
        Vector2 Size { get; set; }
        SingleGamePlayer _boss;
        SingleGamePlayer _player;
        List<Sprite> bgList;
        List<Unitity.Constants.ItemTypes> list;
        public StageItemListBar(SingleGamePlayer player, SingleGamePlayer boss)
        {
            _boss = boss;
            _player = player;
            Size = new Vector2(320, 74);
            ZIndex = Constants.ui;
        }

        public override void OnLoad()
        {
            base.OnLoad();
            list = Game1.stageManager.GameSrate.ChooseItem;
            bgList = new List<Sprite>();
            Vector2 basepos = new Vector2(Size.X, Size.Y / 2);
            for (int i = list.Count - 1; i >= 0; i--)
            {
                Sprite bgSprite = new Sprite();
                bgSprite.textureName = "gameItemBar/frame_s";
                bgSprite.ZIndex = Constants.uibackground;
                bgSprite.Origin = new Vector2(0.5f);
                bgSprite.Position = Position + basepos;
                bgSprite.OnLoad();
                this.Attach(bgSprite);
                bgList.Add(bgSprite);

                Item item = new Item();
                item.textureName = "gameItemBar/items";
                item.OnLoad();
                item.ItemType = list[i];
                item.Position = Position + basepos;
                basepos.X -= 80;
                item.ZIndex = Constants.ui;
                TouchManager.RegisteTouchArea(item);
                item.OnTouchEvent += OnItemTouch;
                //item.OnReleaseEvent += item.RemoreMyself;
                //item.OnReleaseEvent += UpdateItemList;
                //_scene.TouchManager.RegisteTouchArea(item);
                Attach(item);
            }
        }

        void OnItemTouch(BaseEntity entity, TouchLocation touchLocation) 
        {
            Item i = (Item)entity;
           
            switch (i.ItemType) 
            {
                case Constants.ItemTypes.Marble:  //0
                    _boss.MinusLifeTime(10);
                    Game1.stageManager.GameSrate.BuyedItems[0]--;
                    break;
                case Constants.ItemTypes.Sandbag: //1
                    _boss.MinusLifeTime(20);
                    Game1.stageManager.GameSrate.BuyedItems[1]--;
                    break;
                case Constants.ItemTypes.Copter:  //2
                    _player.AddLifeTime(10);
                    Game1.stageManager.GameSrate.BuyedItems[2]--;
                    break;
                case Constants.ItemTypes.Windmill://3
                    _player.AddLifeTime(20);
                    Game1.stageManager.GameSrate.BuyedItems[3]--;
                    break;
                case Constants.ItemTypes.Doll:    //4
                    _player.AddLifeTime(30);
                    Game1.stageManager.GameSrate.BuyedItems[4]--;
                    break;
            }
            i.Visable = false;
            bgList[list.Count-1 - list.IndexOf(i.ItemType)].Visable = false;
        }

        public override BaseScene Clone()
        {
            throw new NotImplementedException();
        }
    }
}
